package filter;

import com.alibaba.fastjson.JSON;
import entity.ResultVO;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author Kevin
 * @date 2020-12-29 19:19
 */
@WebFilter(urlPatterns = "/*")
public class ExceptionAdviceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            byte[] bytes = JSON.toJSONBytes(ResultVO.error("【老王牛逼】" + e.getMessage()));
            response.setContentType("application/json");
            response.getOutputStream().write(bytes);
        }
    }

    @Override
    public void destroy() {

    }
}
